package com.martin.lib_base.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.martin.lib_base.R
import com.martin.lib_base.base.BaseActivity
import com.martin.lib_base.bean.PreviewBean
import com.martin.lib_base.common.CommonAdapter
import com.martin.lib_base.constant.ConstantKey
import com.martin.lib_base.databinding.ActivityPreviewBinding
import com.martin.lib_base.impl.EmptyViewModel
import com.martin.lib_base.ktx.toJson
import com.martin.lib_base.net.NetApply
import com.martin.lib_base.ui.holder.PreviewImageViewHolder
import com.xuexiang.xui.utils.StatusBarUtils

/**
 * 图片预览界面
 */
class PreviewActivity : BaseActivity<ActivityPreviewBinding,EmptyViewModel>() {

    override fun getViewModelClass() = EmptyViewModel::class.java

    // 适配器
    private val adapter = CommonAdapter(
        requireContext(), listOf(
            PreviewImageViewHolder::class.java
        )
    )

    override fun beforeSetContentView() {
        StatusBarUtils.translucent(this)
    }

    override fun layoutId() = R.layout.activity_preview

    override fun initOnCreate(savedInstanceState: Bundle?) {
        // 获取传递过来的数据
        intent.getStringExtra(ConstantKey.JSON)?.let {
            val list = NetApply.gson.fromJson(it, Array<PreviewBean>::class.java).toList()
            adapter.refresh(list)
        }
        // 设置适配器
        binding.vp.adapter = adapter

        // 设置当前显示的位置
        binding.vp.currentItem = intent.getIntExtra(ConstantKey.POSITION, 0)

        // 关闭
        binding.ivClose.setOnClickListener {
            finish()
        }
    }

    companion object {
        @JvmStatic
        fun start(context: Context, list: List<PreviewBean>, position: Int = 0) {
            val starter = Intent(context, PreviewActivity::class.java)
                .putExtra(ConstantKey.JSON, list.toJson(false))
                .putExtra(ConstantKey.POSITION, position)
            context.startActivity(starter)
        }
    }
}